package com.wuye.parking.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wuye.charge.service.impl.WyCostServiceImpl;
import com.wuye.parking.domain.WyRegion;
import com.wuye.parking.entity.TimeRange;
import com.wuye.parking.service.IWyRegionService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wuye.common.annotation.Log;
import com.wuye.common.core.controller.BaseController;
import com.wuye.common.core.domain.AjaxResult;
import com.wuye.common.enums.BusinessType;
import com.wuye.parking.domain.WyParkingLot;
import com.wuye.parking.service.IWyParkingLotService;
import com.wuye.common.utils.poi.ExcelUtil;
import com.wuye.common.core.page.TableDataInfo;

/**
 * 车位查询Controller
 * 
 * @author avalon
 * @date 2024-11-23
 */
@RestController
@RequestMapping("/parking/carLocation")
public class WyParkingLotController extends BaseController
{
    @Autowired
    private IWyParkingLotService wyParkingLotService;
    @Autowired
    private IWyRegionService iwyRegionService;
    @Autowired
    private WyCostServiceImpl wyCostService;
    /**
     * 根据costid查询cost
     */
    @PreAuthorize("@ss.hasPermi('parking:carLocation:list')")
    @GetMapping("/queryCost/{costId}")
    public AjaxResult queryCost(@PathVariable("costId") Long costId)
    {
        return success(wyCostService.selectWyCostByCostId(costId));
    }
    /**
     * 查询车位查询列表
     */
    @PreAuthorize("@ss.hasPermi('parking:carLocation:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyParkingLot wyParkingLot)
    {
        startPage();
        List<WyParkingLot> list = wyParkingLotService.selectWyParkingLotList(wyParkingLot);
        for (WyParkingLot wyParkingLot1 : list) {
            WyRegion wyRegion = iwyRegionService.selectWyRegionByRegionId(wyParkingLot1.getRegionId());
            wyParkingLot1.setRegion(wyRegion);
        }
        return getDataTable(list);
    }
    /**
     * 根据日期计算计费总数
     */

@PreAuthorize("@ss.hasPermi('parking:carLocation:list')")
@PostMapping("/queryTotal")
public AjaxResult queryTotal(@RequestBody TimeRange timeRange) {
    String startTime = timeRange.getStartTime();
    String endTime = timeRange.getEndTime();
    // 定义时间格式
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    // 将字符串转换为 LocalDateTime 对象
    LocalDate startDateTime = LocalDate.parse(startTime, formatter);
    LocalDate endDateTime = LocalDate.parse(endTime, formatter);
    // 计算两个日期之间的天数
    long daysBetween = ChronoUnit.DAYS.between(startDateTime, endDateTime);

    return AjaxResult.success(daysBetween);
}

    /**
     * 查询所有的车位信息 不分页
     */
    @PreAuthorize("@ss.hasPermi('parking:carLocation:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(WyParkingLot wyParkingLot)
    {
        List<WyParkingLot> list = wyParkingLotService.selectWyParkingLotList(wyParkingLot);
        for (WyParkingLot wyParkingLot1 : list) {
            WyRegion wyRegion = iwyRegionService.selectWyRegionByRegionId(wyParkingLot1.getRegionId());
            wyParkingLot1.setRegion(wyRegion);
        }
        return getDataTable(list);
    }
    /**
     * 导出车位查询列表
     */
    @PreAuthorize("@ss.hasPermi('parking:carLocation:export')")
    @Log(title = "车位查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyParkingLot wyParkingLot)
    {
        List<WyParkingLot> list = wyParkingLotService.selectWyParkingLotList(wyParkingLot);
        ExcelUtil<WyParkingLot> util = new ExcelUtil<WyParkingLot>(WyParkingLot.class);
        util.exportExcel(response, list, "车位查询数据");
    }

    /**
     * 获取车位查询详细信息
     */
    @PreAuthorize("@ss.hasPermi('parking:carLocation:query')")
    @GetMapping(value = "/{parkId}")
    public AjaxResult getInfo(@PathVariable("parkId") Long parkId)
    {
        return success(wyParkingLotService.selectWyParkingLotByParkId(parkId));
    }

    /**
     * 新增车位查询
     */
    @PreAuthorize("@ss.hasPermi('parking:carLocation:add')")
    @Log(title = "车位查询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyParkingLot wyParkingLot)
    {
        return toAjax(wyParkingLotService.insertWyParkingLot(wyParkingLot));
    }

    /**
     * 修改车位查询
     */
    @PreAuthorize("@ss.hasPermi('parking:carLocation:edit')")
    @Log(title = "车位查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyParkingLot wyParkingLot)
    {
        return toAjax(wyParkingLotService.updateWyParkingLot(wyParkingLot));
    }

    /**
     * 删除车位查询
     */
    @PreAuthorize("@ss.hasPermi('parking:carLocation:remove')")
    @Log(title = "车位查询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{parkIds}")
    public AjaxResult remove(@PathVariable Long[] parkIds)
    {
        return toAjax(wyParkingLotService.deleteWyParkingLotByParkIds(parkIds));
    }
    /**
     * 查询某个小区下车位的总数和租用中车位的总数
     */
    @PreAuthorize("@ss.hasPermi('parking:carLocation:list')")
    @GetMapping("/selectParkingAndRentParkingTotal/{communityId}")
    public AjaxResult selectParkingAndRentParkingTotal(@PathVariable Long communityId)
    {
        return success(wyParkingLotService.selectParkingAndRentParkingTotal(communityId));
    }
}
